﻿namespace flyqi
{
    internal class Program
    {
        static void Main(string[] args)
        {
            #region 1. 控制台初始化
            int w = 50;
            int h = 30;
            ConsoleInit(w,h);
            Console.CursorVisible = false;
            #endregion
            #region 2. 场景选择相关
            // 申明一个表示场景标识的变量
            E_SceneType nowSceneType = E_SceneType.Begin;
            while (true)
            {
                switch (nowSceneType)
                {
                    case E_SceneType.Begin:
                        //开始场景逻辑
                        Console.Clear();
                        BeginScene(w,h,ref nowSceneType);
                        break;
                    case E_SceneType.End:
                        //结束场景逻辑
                        Console.Clear();
                        //通过函数处理游戏的场景逻辑
                        break;
                    case E_SceneType.Game:
                        Console.Clear();
                        GameScene(w, h);
                        //游戏场景逻辑
   
                        break;
                }
            }
            #endregion
        }

        #region 1.控制台初始化
        static void ConsoleInit(int w, int h)
        {
            // 基础设置
            // 光标的隐藏
            Console.CursorVisible = true;
            // 舞台的大小
            Console.SetWindowSize(w, h);
            Console.SetBufferSize(w, h);
        }
        #endregion


        #region 1.开始场景逻辑初始化
        static void BeginScene(int w,int h, ref E_SceneType nowSceneType)
        {
            // 设置光标位置
            Console.SetCursorPosition(w/2-3, 8);
            Console.Write("飞行棋");
            // 当前选项的编号
            int nowSelIndex = 0;
            bool isQuit = false;
            // 开始场景逻辑处理循环
            while (true)
            {
                Console.SetCursorPosition(w / 2 - 4, 13);
                Console.ForegroundColor=nowSelIndex ==0 ? ConsoleColor.Red : ConsoleColor.White;
                Console.Write("开始游戏");
                Console.SetCursorPosition(w / 2 - 4, 15);
                Console.ForegroundColor = nowSelIndex == 1 ? ConsoleColor.Red : ConsoleColor.White;
                Console.Write("结束游戏");

                switch (Console.ReadKey(true).Key) {
                    case ConsoleKey.W:
                        --nowSelIndex;
                        if (nowSelIndex <0) { 
                            nowSelIndex = 0;
                        }
                        break;
                    case ConsoleKey.S:
                        ++nowSelIndex;
                        if (nowSelIndex > 1)
                        {
                            nowSelIndex = 1;
                        }
                        break;
                    case ConsoleKey.J:
                        if (nowSelIndex == 0)
                        {
                            // 进入游戏
                            //1 改变当前场景ID
                            nowSceneType = E_SceneType.Game;
                            //2 退出当前循环
                            isQuit=true;
                        }
                        else 
                        {
                            // 退出游戏
                            Environment.Exit(0);

                        }
                        break;
                }

                if (isQuit)
                {
                    break;
                }
            }
        }
        #endregion

        #region  游戏场景逻辑
        static void GameScene(int w,int h) {
            //绘制不变的信息
            DrawWell(w, h);
            //绘制地图
            Grid grid = new Grid(5,5,E_Grid_Type.Normal);
            grid.Draw();
            Grid grid1 = new Grid(5, 6, E_Grid_Type.Pause);
            grid1.Draw();
            Grid grid2 = new Grid(5, 7, E_Grid_Type.Tunnel);
            grid2.Draw();
            Grid grid3 = new Grid(5, 8, E_Grid_Type.Boom);
            grid3.Draw();
            // 绘制玩家

            // 游戏场景循环
            while (true)
            {

            }
        }
        #endregion 


        #region 绘制不变的信息（红墙）
        static void DrawWell(int w, int h)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            // 画墙
            for (int i = 0; i < w; i+=2)
            {
                Console.SetCursorPosition(i, 0);
                Console.Write("■");
                Console.SetCursorPosition(i, h-1);
                Console.Write("■");

                Console.SetCursorPosition(i, h-6);
                Console.Write("■");

                Console.SetCursorPosition(i, h-11);
                Console.Write("■");
            }

            for (int i = 0; i < h; i++)
            {
                Console.SetCursorPosition(0, i);
                Console.Write("■");

                Console.SetCursorPosition(w-2, i);
                Console.Write("■");
            }


            // 文字信息
            Console.ForegroundColor = ConsoleColor.White;
            Console.SetCursorPosition(2, h - 10);
            Console.Write("□:普通格子");

            Console.ForegroundColor = ConsoleColor.Blue;
            Console.SetCursorPosition(2, h - 9);
            Console.Write("‖:暂停,一回合不动");

            Console.ForegroundColor = ConsoleColor.Red;
            Console.SetCursorPosition(26, h - 9);
            Console.Write("●:炸弹,倒退五格");

            Console.ForegroundColor = ConsoleColor.White;
            Console.SetCursorPosition(2, h - 8);
            Console.Write("¤:时空隧道,随机倒退,暂停,换位置");

            Console.ForegroundColor = ConsoleColor.Cyan;
            Console.SetCursorPosition(2, h - 7);
            Console.Write("★:玩家");

            Console.ForegroundColor = ConsoleColor.Magenta;
            Console.SetCursorPosition(10, h - 7);
            Console.Write("▲:电脑");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.SetCursorPosition(18, h - 7);
            Console.Write("◎:玩家与电脑重合");


            Console.ForegroundColor = ConsoleColor.White;
            Console.SetCursorPosition(2, h - 5);
            Console.Write("按任意键开始扔色子");
        }

        #endregion

    }
    #region 2. 场景选择相关
    /// <summary>
    /// 游戏场景吗，枚举类型
    /// </summary>
    enum E_SceneType
    {
        /// <summary>
        ///  开始场景
        /// </summary>
        Begin,
        /// <summary>
        /// 游戏场景
        /// </summary>
        Game,
        /// <summary>
        /// 结束场景
        /// </summary>
        End,
    }
    #endregion

    #region 格子结构体和格子枚举
    enum E_Grid_Type 
    {
        /// <summary>
        /// 普通格子
        /// </summary>
        Normal,
        /// <summary>
        /// 炸弹
        /// </summary>
        Boom,
        /// <summary>
        /// 暂停
        /// </summary>
        Pause,
        /// <summary>
        /// 时空隧道
        /// </summary>
        Tunnel
    }


    struct Grid
    {
        public E_Grid_Type type;
        public Vector2 pos;
        // 初始化构造函数
        public Grid(int x, int y, E_Grid_Type type)
        {
            pos.x = x; pos.y = y;
            this.type = type;
        }

        public void Draw()
        {
            Console.SetCursorPosition(pos.x, pos.y);
            switch (type)
            {
                // 普通格子 怎么画
                case E_Grid_Type.Normal:
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.Write("□");
                    break;
                // 炸弹格子 怎么画
                case E_Grid_Type.Boom:
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Write("●");
                    break;
                // 暂停格子 怎么画
                case E_Grid_Type.Pause:
                    Console.ForegroundColor = ConsoleColor.Blue;
                    Console.Write("‖");
                    break;
                // 时空隧道格子 怎么画
                case E_Grid_Type.Tunnel:
                    Console.ForegroundColor = ConsoleColor.White;
                    Console.Write("¤");
                    break;
            }
        }
    }

    //位置信息
    struct Vector2
    {
        public int x;
        public int y;

        public Vector2(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
    }
    #endregion


    #region 地图结构体

    struct Map 
    {
        public Grid[] grids;

        public Map(int x,int y,int num)
        {
            grids = new Grid[num];
            Random r = new Random();
            // 用于改变计数的变量
            int indexX = 0;
            int indexY = 0;
            int stepNum = 2;
            int randomNum;
            for (int i = 0; i < num; i++)
            {
                // 应该初始化 格子类型
                randomNum = r.Next(0,101);

                //设置类型
                if (randomNum < 85 || i == 0 || i == num - 1)
                {
                    grids[i].type = E_Grid_Type.Normal;
                }
                else if (randomNum >= 85 && randomNum < 90)
                {
                    grids[i].type = E_Grid_Type.Boom;
                }
                else if (randomNum >= 90 && randomNum < 95)
                {
                    grids[i].type = E_Grid_Type.Pause;
                }
                else {
                    grids[i].type = E_Grid_Type.Tunnel;
                }

                // 位置 
                grids[i].pos = new Vector2(x,y);

                if (indexX == 10)
                {
                    y += 1;
                    ++indexY;
                    if (indexY == 2) { 
                        indexX = 0;
                        indexY = 0;

                        stepNum=-stepNum;
                    }
                }
                else 
                {
                    x += stepNum;
                    ++indexX;
                }
              
                
               
            }
        }
    }

    #endregion

}
